package com.zcp.greendaodemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.zcp.greendaodemo.bean.user;
import com.zcp.greendaodemo.greendao.DaoSession;
import com.zcp.greendaodemo.greendao.userDao;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DaoSession db = App.getDb();
        userDao userDao2 = db.getUserDao();
        userDao2.insert(new user(null, "zcp2", 25, "女"));
        userDao2.insert(new user(null, "wjw1", 25, "男"));
        userDao2.insert(new user(null, "wa", 26, "男"));
        userDao2.insert(new user(null, "12", 24, "男"));
        userDao2.insert(new user(null, "qeq", 25, "男"));
        userDao2.insert(new user(null, "werwr", 29, "男"));

        List<user> users = userDao2.loadAll();
        //Log.d("zzz",users.toString());
        //userDao2.update(new user((long) 2,"aass",21,"man"));

        //根据id查询
        //user user = userDao2.load((long) 2);
        //Log.d("zzz",user.toString());

        //条件查询
        List<com.zcp.greendaodemo.bean.user> list = userDao2.queryBuilder().where(userDao.Properties.Age.eq(24)).list();
        Log.d("zzz", list.toString());
        for (user u : list) {
            userDao2.delete(u);
        }

        //删除
//        deleteBykey(Long key) ：根据主键删除一条记录。
//        delete(User entity) ：根据实体类删除一条记录，一般结合查询方法，查询出一条记录之后删除。
//        deleteAll()： 删除所有记录。
//        userDao2.deleteByKey((long)8);
//
        List<user> users1 = userDao2.loadAll();
        for (user uu : users1) {
            Log.d("Tag", uu.toString());
        }

//        查
//
//        loadAll()：查询所有记录
//        load(Long key)：根据主键查询一条记录
//        queryBuilder().list()：返回：List
//        queryBuilder().where(UserDao.Properties.Name.eq("")).list()：返回：List
//        queryRaw(String where,String selectionArg)：返回：List


    }
}
